package com.servlet;

import com.entity.User;

import com.mapper.UserMapper;
import com.util.CryptoUtils;
import com.util.MybatiesUtil;
import org.apache.ibatis.session.SqlSession;
import sell.DbTest;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by webrx on 2017/6/15.
 */
@WebServlet("/usertable.do")
public class UserTable extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        PrintWriter out =resp.getWriter();

        MybatiesUtil mu = new MybatiesUtil();
        SqlSession s =mu.getSession();
        UserMapper user = s.getMapper(UserMapper.class);

        String action =req.getParameter("action");

        switch (action) {
            case "yan":

                   String av = req.getParameter("av");
                   User uu = user.queryByName(av);

                 if (uu != null) {
                   out.print("no");
                  System.out.println("no");
                } else {
                  System.out.println("yes");
                  out.print("yes");
                }
                 out.flush();
                out.close();
                 break;

            case "add":
               /* DbTest db = new DbTest();*/
                String name=req.getParameter("n");
                String pass=req.getParameter("p");
                String gender=req.getParameter("g");
                //获取动态密码
                String pas = CryptoUtils.getHash(pass,name);




                User us=new User();
                us.setName(name);
                us.setGender(gender);
                us.setPassword(pas);


                  int nn = user.add(us);

                 s.commit();
                 s.close();
                System.out.println(nn);
                  if(nn>0){

                      out.print("恭喜您注册成功");

                  }
                break;

        }

    }
}
